package com.ufenlei.alarm
{
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	
	import mx.collections.ArrayCollection;
		
	public class QuickAddManager
	{
		[Bindable]
		public var list:ArrayCollection;
		private var deleteItem:QuickAddItem;
		private static var _instance:QuickAddManager = null;
		
		public function QuickAddManager(sing:Single)
		{
			this.list = new ArrayCollection;
			this.DBRead();
		}
		
		public static function getInstance():QuickAddManager{
			if(_instance == null){
				_instance = new QuickAddManager(new Single());
			}
			return _instance;
		}

		public function add(qa:QuickAddItem):void{
			this.list.addItem(qa);
			if(!qa.id) this.DBSave(qa);
		}
		public function remove(qa:QuickAddItem):void{
			this.deleteItem = qa;
			this.DBDelete(qa);
		}
		private function DBSave(qa:QuickAddItem):void{
			var sql:String = "INSERT INTO quick_add (title,time,is_period) VALUES(:title,:time,:isperiod)";
			var addItemStmp:SQLStatement = new SQLStatement();
			addItemStmp.sqlConnection = LocalDB.getInstance().conn;
			addItemStmp.text = sql;
			addItemStmp.parameters[':title'] = qa.label;
			addItemStmp.parameters[':time'] = qa.at.time;
			addItemStmp.parameters[':isperiod'] = qa.at.isPeriod?1:0;
			trace(addItemStmp.text);
			addItemStmp.addEventListener(SQLEvent.RESULT,onSaveResult);
			addItemStmp.addEventListener(SQLErrorEvent.ERROR,onError);
			addItemStmp.execute();
		}
		private function DBRead():void{
			var sql:String = "SELECT * FROM quick_add ";
			var readItemsStmp:SQLStatement = new SQLStatement();
			readItemsStmp.sqlConnection = LocalDB.getInstance().conn;
			readItemsStmp.text = sql;
			readItemsStmp.addEventListener(SQLEvent.RESULT,onReadResult);
			readItemsStmp.addEventListener(SQLErrorEvent.ERROR,onReadERR);
			readItemsStmp.execute();			
		}
		private function DBDelete(qa:QuickAddItem):void{
			if(qa.id){
				var sql:String = "DELETE FROM quick_add WHERE id = :id";
				var stmp:SQLStatement = new SQLStatement();
				stmp.text = sql;
				stmp.parameters[':id'] = qa.id;
				stmp.sqlConnection = LocalDB.getInstance().conn;
				stmp.addEventListener(SQLEvent.RESULT,onDeleteResult);
				stmp.addEventListener(SQLErrorEvent.ERROR,onError);
				stmp.execute();	
			}
		}
		private function onDeleteResult(evt:SQLEvent):void{
			this.list.removeItemAt(this.list.getItemIndex(this.deleteItem));
		}
		private function onSaveResult(evt:SQLEvent):void
		{
			
		}
		private function onError(evt:SQLErrorEvent):void{
			trace(evt.error.message," #",evt.errorID);
		}
		private function onReadResult(evt:SQLEvent):void
		{
			var stmp:SQLStatement = evt.target as SQLStatement;
			var result:SQLResult = stmp.getResult();
			if(result.data){
				var length:uint = result.data.length;
				for(var i:uint = 0; i < length; i++){
					var item:Object = result.data[i];
					var isp:Boolean = (item.is_period == 1)?true:false;
					this.add(new QuickAddItem(new AlarmTime(item.time,isp),item.title,item.id));
				}
			}
		}
		private function onReadERR(evt:SQLErrorEvent):void{
			trace(evt.error.message);
		}
	}
}
class Single{}